<?php
// $Id: database.mysql.inc,v 1.89 2008/01/24 10:46:54 goba Exp $

/**
 * @file
 * Database interface code for MySQL database servers.
 */

/**
 * @ingroup database
 * @{
 */

// Include functions shared between mysql and mysqli.
//require_once './includes/database.mysql-common.inc';

if(!function_exists('db_status_report')) {
  require_once './includes/database.mysql.inc';
}
/**
 *A database abstraction class for performing common operations on a database.
 *
 */
Class KDatabase {
  protected $Table;  //Active table
  public function __construct($table='') {
    if($table){
      $this->Table = $table;
    }    
  }
  
  public function SetTable($table){
    $this->Table = $table;
    return $this->Table;
  }

  public function GetTable(){
    return $this->Table;
  }
  
  public function InsertRow(&$row) {
    $row = (object)$row;
    drupal_get_schema($this->Table, 1);  # Forcibly rebuild the schema cache
    return drupal_write_record($this->Table, $row);    
  }
  
  public function RetrieveRow($options = array('wheres' => array(), 
      'debug' => FALSE)) {
    $options['limit'] = 1;
    $rows = $this->RetrieveRows($options);
    return $rows[0];
  }
  
  public function UpdateRow(&$row) {
    $row = (object)$row;
    $schema = drupal_get_schema($this->Table);    
    return drupal_write_record($this->Table, $row, $schema['primary key']);
  }
  
  public function DeleteRow($key) {
    
  }
  
  public function InsertRows($rows) {
    
  }
  
  
  public function RetrieveRows($options = array('cols' => array('*'), 
      'wheres' => array(), 'limit' => 100, 'debug' => FALSE, 'after' => 0)) {
    global $user;
    # Note that cache_key only depends on the WHERE clause. If found to be
    # necessary in practice, also cache by ORDER BY clause, and GROUP BY clause
    $cache_key = $this->Table.':'.
                  str_replace(' ', '',
                              join($options['wheres']));
    $until = time() + $after;
    if($user->uid==0 && mb_strlen($cache_key < 255)) {
    #  Only cache for anon users. Make sure that cache key can fit
      $cache = cache_get($cache_key);
      if(is_array($cache->data) && count($cache->data) > 0) {
        return $cache->data;
      }
    }

    $q_fxn = $options['debug']?'db_queryd':'db_query';
    $rows = array();
    $options['limit'] = isset($options['limit'])? $options['limit']:
        variable_get('fnsql_default_limit', 100);
    $options['wheres'][] = 1;  //Default
    $q = @$q_fxn('SELECT %s FROM %s WHERE '.join(' AND ', $options['wheres']).
                ' %s %s LIMIT %s',
        (!empty($options['cols'])?join(', ', $options['cols']):'*'), 
        db_prefix_tables("{{$this->Table}}"), # Force curly brackets to be replaced 
        (!empty($options['group'])?('GROUP BY '.join(', ',
                                                     $options['group'])):''),
        (!empty($options['order'])?('ORDER BY '.join(', ',
                                                     $options['order'])):''),
        $options['limit']);
    while($r = db_fetch_object($q)) {
      $rows[] = $r;
    }

    if(is_array($rows) && count($rows) > 0) {
      cache_set($cache_key, $rows, 'cache', $until);
    }
    return $rows;
  }
  
  public function UpdateRows($rows) {
    
  }
  
  public function DeleteRows($wheres, $limit=10, $debug=FALSE) {
    $q_fxn = $debug?'db_queryd':'db_query';
    $rows = array();
    $conditions = array();    
    foreach($wheres as $col => $value) {
      $conditions[] = sprintf('%s=%s', $col, db_escape_string($value));
    }
    $q = $q_fxn('DELETE FROM {%s} WHERE %s LIMIT %d', $this->Table,
                join(' AND ', $conditions), $limit);
    return $q;
  }
  
  public function CleanUpTable($table) {
    
  }
  
  public function CleanUpTables($tables) {
    
  }
}
/**
 * @} End of "ingroup database".
 */
